<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Ajax extends Controller {

	function Ajax ()
	{
		parent::Controller();	
		$this->load->model('ajax_model');
		$this->load->library('flexigrid');
	}
	
	function index()
	{
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('id','iso','name','printable_name','iso3','numcode');
		
		$this->flexigrid->validate_post('id','asc',$valid_fields);

		$records = $this->ajax_model->get_countries();
		
		$this->output->set_header($this->config->item('fg_json_header'));
		
		//Init json build
		if ($this->flexigrid->init_json_build($records['record_count'])) 
		{
			//Add records
			foreach ($records['records']->result() as $row)
			{
				$record_item = array($row->id,
				$row->id,
				$row->iso,
				$row->name,
				'<span style=\'color:#ff4400\'>'.addslashes($row->printable_name).'</span>',
				$row->iso3,
				$row->numcode,
				'<a href=\'#\'><img border=\'0\' src=\''.$this->config->item('base_url').'public/images/add.png\'></a> <a href=\'#\'><img border=\'0\' src=\''.$this->config->item('base_url').'public/images/close.png\'></a> '
				);
				$this->flexigrid->json_add_item($record_item);
			}
			//Last item added, close up.
			$this->flexigrid->json_add_item();
		}
		
		//Print please
		$this->output->set_output($this->flexigrid->json_build);
	}
}
?>